package com.cmcc.xdr.message;

import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.cmcc.xdr.entity.SipCallEntity;
import com.cmcc.xdr.entity.SipRegEntity;
import com.cmcc.xdr.entity.SipSrvccEntity;
import com.cmcc.xdr.util.TBCDUtil;

public class SingleSigSipMsg240 extends SingleSigMsg {

	public int procedureType;
	public SipCallEntity sipcall = new SipCallEntity();
	public SipRegEntity sipreg = new SipRegEntity();
	public SipSrvccEntity sipSrvcc = new SipSrvccEntity();

	private static SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
	
	private static Logger logger = LogManager.getLogger(SingleSigSipMsg240.class);

	public SingleSigSipMsg240(ByteBuffer buffer,SingleSigGlobalMsg globalMsg) {
		this.setBuffer(buffer);
		this.setSSGMsg(globalMsg);

		sipcall = new SipCallEntity();
		sipreg = new SipRegEntity();
		procedureType = loadInt(1);
		switch (procedureType) {
		case 1:
		case 2:
		case 3:
		case 4:
			sipreg.setLength(SSGMsg.getTotalLength());
			sipreg.setLocalProvince(SSGMsg.getLocalProvince());
			sipreg.setLocalCity(SSGMsg.getLocalCity());
			sipreg.setOwnerProvince(SSGMsg.getOwnerProvince());
			sipreg.setOwnerCity(SSGMsg.getOwnerCity());
			sipreg.setRoamingType(SSGMsg.getRoamingType());
			sipreg.setInterFace(SSGMsg.getInterfaceType());
			sipreg.setXdrId(SSGMsg.getXdrID());
			sipreg.setRat(SSGMsg.getRat());
			sipreg.setImsi(SSGMsg.getImsi());
			sipreg.setImei(SSGMsg.getImei());
			sipreg.setMsisdn(SSGMsg.getMsisdn());
			sipreg.setProcedureType(procedureType);
			sipreg.setProcedureStartTime(new Date(loadLong(8)));
			sipreg.setProcedureEndTime(new Date(loadLong(8)));
			sipcall.setServiceType(loadInt(1));
			sipreg.setProcedureStatus(loadInt(1));
			sipcall.setCallingNumber(TBCDUtil.toTBCD(loadByte(16)));
			sipcall.setCalledNumber(TBCDUtil.toTBCD(loadByte(16)));
			sipcall.setCallingPartyUri(loadStr(32));
			sipreg.setRequestUri(loadStr(32));
			sipreg.setUserIp(loadIp(16));
			sipreg.setCallid(loadStr(64));
			sipreg.setIcid(loadStr(64));
			sipreg.setSourceNeIp(loadIp(16));
			sipreg.setSourceNePort(loadInt(2));
			sipreg.setDestNeIp(loadIp(16));
			sipreg.setDestNePort(loadInt(2));
			sipcall.setCallSide(loadInt(1));
			sipreg.setSourceAccessType(loadInt(1));
			sipreg.setSourceEci(loadStrInt(4));
			sipreg.setSourceTac(loadStrInt(2));
			sipcall.setSourceImsi(TBCDUtil.toTBCD(loadByte(8)));
			sipcall.setSourceImei(TBCDUtil.toTBCD(loadByte(8)));
			//----------2.4.0规范新加字段-start---------
			sipcall.setSourceSbcDomain(loadStr(32));
			//----------2.4.0规范新加字段-end-----------
			sipcall.setDestAccessType(loadInt(1));
			sipcall.setDestEci(loadHexStr(4));
			sipcall.setDestTac(loadHexStr(2));
			sipcall.setDestImsi(TBCDUtil.toTBCD(loadByte(8)));
			sipcall.setDestImei(TBCDUtil.toTBCD(loadByte(8)));
			//----------2.4.0规范新加字段-start---------
			sipcall.setDestSbcDomain(loadStr(32));
			//----------2.4.0规范新加字段-end-----------
			sipreg.setAuthType(loadInt(1));
			sipreg.setExpiresTimeReq(loadInt(4));
			sipreg.setExpiresTimeRsp(loadInt(4));
			sipcall.setCallingSdpIpAddr(loadIp(16));
			sipcall.setCallingAudioSdpPort(loadInt(2));
			sipcall.setCallingVideoSdpPort(loadInt(2));
			sipcall.setCalledSdpIpAddr(loadIp(16));
			sipcall.setCalledAudioSdpPort(loadInt(2));
			sipcall.setCalledVideoPort(loadInt(2));
			sipcall.setAudioCodec(loadInt(1));
			sipcall.setVideoCodec(loadInt(1));
			sipcall.setRedirectingPartyAddress(loadStr(32));
			sipcall.setOriginalPartyAddress(loadStr(32));
			//----------2.4.0规范新加字段-start---------
			sipcall.setPrivacy(loadStr(6));
			//----------2.4.0规范新加字段-end-----------
			sipcall.setRedirectReason(loadInt(1));
			sipreg.setResponseCode(loadInt(2));
			sipreg.setFinishWarningCode(loadInt(2));
			sipreg.setFinishReasonProtocol(loadInt(1));
			sipreg.setFinishReasonCause(loadInt(2));
			//----------2.4.0规范新加字段-start-----------
			sipreg.setCancelReasonProtocal(loadInt(1));
			sipreg.setCancelReasonCause(loadInt(2));
			sipreg.setReasonProtocolof183(loadInt(1));
			sipreg.setReasonCauseof183(loadInt(2));
			sipSrvcc.setCallFailEnd(loadInt(1)); 
			//----------2.4.0规范新加字段-end-----------
			sipcall.setFirfailtime(loadInt(4));
			sipcall.setFirstFailNeIp(loadIp(16));
			sipcall.setFirstFailTransaction(loadInt(1));//新增字段
			sipcall.setProgressTime(loadInt(4));//新增字段：呼叫处理时长
			//----------2.4.0规范新加字段-start-----------
			sipcall.setUpdateTime1(loadInt(4)); 
			sipcall.setUpdateTime2(loadInt(4)); 
			sipcall.setUpdateTime3(loadInt(4)); 
			//----------2.4.0规范新加字段-end-----------
			sipcall.setPrackTime(loadInt(4));//新增字段：Prack时长
			sipcall.setAlertingTime(loadInt(4));
			sipcall.setAnswerTime(loadInt(4));
			sipcall.setReleaseTime(loadInt(4));
			sipcall.setCallDuration(loadInt(4));
			sipreg.setAuthReqTime(loadInt(4));
			sipreg.setAuthRspTime(loadInt(4));
			sipreg.setStnSr(TBCDUtil.toTBCD(loadByte(24)));
			sipreg.setAtcfMgmt(loadStr(64));
//			logger.error(sipreg.getXdrId()+"--sipreg.getAtcfMgmt()----"+sipreg.getAtcfMgmt());
			loadStr(64);//存在一个message里有多个xdr，最后几个字段还是需要load ATU-STI
			loadByte(16);//C-MSISDN
			loadHexStr(16);//SSI
			// /////////////////////////////////////////////////
			//----------2.4.0规范新加字段-start-----------
			sipreg.setRetryAfter(loadInt(4)); 
			sipcall.setReleasePart(loadInt(4)); 
			sipreg.setUserAgent(loadStr(32)); 
			//----------2.4.0规范新加字段-end-----------
			sipreg.setInsertDate(format.format(new Date()));

			// dbtest.saveSipReg(sipreg);
			break;
		case 5:
			sipcall.setLength(SSGMsg.getTotalLength());
			sipcall.setLocalProvince(SSGMsg.getLocalProvince());
			sipcall.setLocalCity(SSGMsg.getLocalCity());
			sipcall.setOwnerProvince(SSGMsg.getOwnerProvince());
			sipcall.setOwnerCity(SSGMsg.getOwnerCity());
			sipcall.setRoamingType(SSGMsg.getRoamingType());
			
			sipcall.setInterFace(SSGMsg.getInterfaceType());
			sipcall.setXdrId(SSGMsg.getXdrID());
			sipcall.setRat(SSGMsg.getRat());
			sipcall.setImsi(SSGMsg.getImsi());
			sipcall.setImei(SSGMsg.getImei());
			sipcall.setMsisdn(SSGMsg.getMsisdn());

			sipcall.setProcedureType(procedureType);

			sipcall.setProcedureStartTime(new Date(loadLong(8)));
			sipcall.setProcedureEndTime(new Date(loadLong(8)));
			sipcall.setServiceType(loadInt(1));
			sipcall.setProcedureStatus(loadInt(1));
			sipcall.setCallingNumber(TBCDUtil.toTBCD(loadByte(16)));
			sipcall.setCalledNumber(TBCDUtil.toTBCD(loadByte(16)));
			sipcall.setCallingPartyUri(loadStr(32));
			sipcall.setRequestUri(loadStr(32));
			sipcall.setUserIp(loadIp(16));
			sipcall.setCallId(loadStr(64));
			sipcall.setIcId(loadStr(64));
			sipcall.setSourceNeIp(loadIp(16));
			sipcall.setSourceNePort(loadInt(2));
			sipcall.setDestNeIp(loadIp(16));
			sipcall.setDestNePort(loadInt(2));
			sipcall.setCallSide(loadInt(1));
			sipcall.setSourceAccessType(loadInt(1));
			sipcall.setSourceEci(loadStrInt(4));
			sipcall.setSourceTac(loadStrInt(2));
			sipcall.setSourceImsi(TBCDUtil.toTBCD(loadByte(8)));
			sipcall.setSourceImei(TBCDUtil.toTBCD(loadByte(8)));
			//----------2.4.0规范新加字段-start---------
			sipcall.setSourceSbcDomain(loadStr(32));
			//----------2.4.0规范新加字段-end-----------
			sipcall.setDestAccessType(loadInt(1));
			sipcall.setDestEci(loadStrInt(4));
			sipcall.setDestTac(loadStrInt(2));
			sipcall.setDestImsi(TBCDUtil.toTBCD(loadByte(8)));
			sipcall.setDestImei(TBCDUtil.toTBCD(loadByte(8)));
			//----------2.4.0规范新加字段-start---------
			sipcall.setDestSbcDomain(loadStr(32));
			//----------2.4.0规范新加字段-end-----------
			sipreg.setAuthType(loadInt(1));

			sipreg.setExpiresTimeReq(loadInt(4));

			sipreg.setExpiresTimeRsp(loadInt(4));
			sipcall.setCallingSdpIpAddr(loadIp(16));
			sipcall.setCallingAudioSdpPort(loadInt(2));
			sipcall.setCallingVideoSdpPort(loadInt(2));
			sipcall.setCalledSdpIpAddr(loadIp(16));
			sipcall.setCalledAudioSdpPort(loadInt(2));
			sipcall.setCalledVideoPort(loadInt(2));
			sipcall.setAudioCodec(loadInt(1));
			sipcall.setVideoCodec(loadInt(1));
			sipcall.setRedirectingPartyAddress(loadStr(32));
			sipcall.setOriginalPartyAddress(loadStr(32));
			//----------2.4.0规范新加字段-start---------
			sipcall.setPrivacy(loadStr(6));
			//----------2.4.0规范新加字段-end-----------
			sipcall.setRedirectReason(loadInt(1));
			sipcall.setResponseCode(loadInt(2));
			sipcall.setFinishWarningCode(loadInt(2));
			sipcall.setFinishReasonProtocol(loadInt(1));
			sipcall.setFinishReasonCause(loadInt(2));
			//----------2.4.0规范新加字段-start---------
			sipcall.setCancelReasonProtocal(loadInt(1));
			sipcall.setCancelReasonCause(loadInt(2));
			sipcall.setReasonProtocolof183(loadInt(1));
			sipcall.setReasonCauseof183(loadInt(2));
			sipcall.setCallFailEnd(loadInt(1));
			//----------2.4.0规范新加字段-end-----------
			sipcall.setFirfailtime(loadInt(4));
			sipcall.setFirstFailNeIp(loadIp(16));
			sipcall.setFirstFailTransaction(loadInt(1));
			sipcall.setProgressTime(loadInt(4));//新增字段：呼叫处理时长
			//----------2.4.0规范新加字段-start-----------
			sipcall.setUpdateTime1(loadInt(4)); 
			sipcall.setUpdateTime2(loadInt(4)); 
			sipcall.setUpdateTime3(loadInt(4)); 
			//----------2.4.0规范新加字段-end-----------
			sipcall.setPrackTime(loadInt(4));//新增字段：Prack时长
			sipcall.setAlertingTime(loadInt(4));
			sipcall.setAnswerTime(loadInt(4));
			sipcall.setReleaseTime(loadInt(4));
			sipcall.setCallDuration(loadInt(4));
			sipreg.setAuthReqTime(loadInt(4));
			sipreg.setAuthRspTime(loadInt(4));
			sipreg.setStnSr(loadHexStr(24));
			sipreg.setAtcfMgmt(loadStr(64));
			loadStr(64);//存在一个message里有多个xdr，最后几个字段还是需要load ATU-STI
			loadByte(16);//C-MSISDN
			loadHexStr(16);//SSI
			//----------2.4.0规范新加字段-start-----------
			sipcall.setRetryAfter(loadInt(4)); 
			sipcall.setReleasePart(loadInt(4)); 
			sipcall.setUserAgent(loadStr(32)); 
			//----------2.4.0规范新加字段-end-----------
			sipcall.setInsertDate(format.format(new Date()));
			break;
		case 6:
			sipSrvcc.setLength(SSGMsg.getTotalLength());
			sipSrvcc.setLocalProvince(SSGMsg.getLocalProvince());
			sipSrvcc.setLocalCity(SSGMsg.getLocalCity());
			sipSrvcc.setOwnerProvince(SSGMsg.getOwnerProvince());
			sipSrvcc.setOwnerCity(SSGMsg.getOwnerCity());
			sipSrvcc.setRoamingType(SSGMsg.getRoamingType());
			sipSrvcc.setInterFace(SSGMsg.getInterfaceType());
			sipSrvcc.setXdrId(SSGMsg.getXdrID());
			sipSrvcc.setRat(SSGMsg.getRat());
			sipSrvcc.setImsi(SSGMsg.getImsi());
			sipSrvcc.setImei(SSGMsg.getImei());
			sipSrvcc.setMsisdn(SSGMsg.getMsisdn());
			sipSrvcc.setProcedureType(procedureType);
			sipSrvcc.setProcedureStartTime(new Date(loadLong(8)));
			sipSrvcc.setProcedureEndTime(new Date(loadLong(8)));
			sipcall.setServiceType(loadInt(1));
			sipSrvcc.setProcedureStatus(loadInt(1));
			sipSrvcc.setCallingNumber(TBCDUtil.toTBCD(loadByte(16)));
			sipcall.setCalledNumber(TBCDUtil.toTBCD(loadByte(16)));
			sipSrvcc.setCallingPartyUri(loadStr(32)); 
			sipSrvcc.setRequestUri(loadStr(32));
			sipSrvcc.setUserIp(loadIp(16)); 
			sipSrvcc.setCallId(loadStr(64)); 
			sipSrvcc.setIcId(loadStr(64));
			sipSrvcc.setSourceNeIp(loadIp(16)); 
			sipSrvcc.setSourceNePort(loadInt(2)); 
			sipSrvcc.setDestNeIp(loadIp(16)); 
			sipSrvcc.setDestNePort(loadInt(2)); 
			sipcall.setCallSide(loadInt(1));  
			sipSrvcc.setSourceAccessType(loadInt(1)); 
			sipSrvcc.setSourceEci(loadStrInt(4));
			sipSrvcc.setSourceTac(loadStrInt(2));
			sipSrvcc.setSourceImsi(TBCDUtil.toTBCD(loadByte(8)));
			sipSrvcc.setSourceImei(TBCDUtil.toTBCD(loadByte(8)));
			//----------2.4.0规范新加字段-start---------
			sipcall.setSourceSbcDomain(loadStr(32));
			//----------2.4.0规范新加字段-end-----------
			sipSrvcc.setDestAccessType(loadInt(1)); 
			sipSrvcc.setDestEci(loadHexStr(4));
			sipSrvcc.setDestTac(loadHexStr(2));
			sipSrvcc.setDestImsi(TBCDUtil.toTBCD(loadByte(8))); 
			sipSrvcc.setDestImei(TBCDUtil.toTBCD(loadByte(8)));
			//----------2.4.0规范新加字段-start---------
			sipcall.setDestSbcDomain(loadStr(32));
			//----------2.4.0规范新加字段-end-----------
			sipreg.setAuthType(loadInt(1));             
			sipreg.setExpiresTimeReq(loadInt(4)); 
			sipreg.setExpiresTimeRsp(loadInt(4)); 
			sipSrvcc.setCallingSdpIpAddr(loadIp(16)); 
			sipSrvcc.setCallingAudioSdpPort(loadInt(2));
			sipSrvcc.setCallingVideoSdpPort(loadInt(2));
			sipSrvcc.setCalledSdpIpAddr(loadIp(16));
			sipSrvcc.setCalledAudioSdpPort(loadInt(2)); 
			sipSrvcc.setCalledVideoPort(loadInt(2));
			sipSrvcc.setAudioCodec(loadInt(1)); 
			sipSrvcc.setVideoCodec(loadInt(1));		
			sipcall.setRedirectingPartyAddress(loadStr(32));
			sipcall.setOriginalPartyAddress(loadStr(32));
			//----------2.4.0规范新加字段-start---------
			sipcall.setPrivacy(loadStr(6));
			//----------2.4.0规范新加字段-end-----------
			sipcall.setRedirectReason(loadInt(1));          
			sipSrvcc.setResponseCode(loadInt(2)); 
			sipSrvcc.setFinishWarningCode(loadInt(2));
			sipSrvcc.setFinishReasonProtocol(loadInt(1)); 
			sipSrvcc.setFinishReasonCause(loadInt(2));
			//----------2.4.0规范新加字段-start-----------
			sipSrvcc.setCancelReasonProtocal(loadInt(1));
			sipSrvcc.setCancelReasonCause(loadInt(2));
			sipSrvcc.setReasonProtocolof183(loadInt(1));
			sipSrvcc.setReasonCauseof183(loadInt(2));
			sipSrvcc.setCallFailEnd(loadInt(1)); 
			//----------2.4.0规范新加字段-end-----------
			sipSrvcc.setFirfailtime(loadInt(4));
			sipSrvcc.setFirstFailNeIp(loadIp(16));
			sipSrvcc.setFirstFailTransaction(loadInt(1)); 
			sipcall.setProgressTime(loadInt(4));//新增字段：呼叫处理时长
			//----------2.4.0规范新加字段-start-----------
			sipSrvcc.setUpdateTime1(loadInt(4)); 
			sipSrvcc.setUpdateTime2(loadInt(4)); 
			sipSrvcc.setUpdateTime3(loadInt(4)); 
			//----------2.4.0规范新加字段-end-----------
			sipcall.setPrackTime(loadInt(4));//新增字段：Prack时长
			sipSrvcc.setAlertingTime(loadInt(4)); 
			sipSrvcc.setAnswerTime(loadInt(4)); 
			sipSrvcc.setReleaseTime(loadInt(4));
			sipSrvcc.setCallDuration(loadInt(4)); 
			sipreg.setAuthReqTime(loadInt(4));         
			sipreg.setAuthRspTime(loadInt(4));        
			sipreg.setStnSr(TBCDUtil.toTBCD(loadByte(24)));
			sipreg.setAtcfMgmt(loadStr(64));
			//loadStr(64);//已删除
			sipSrvcc.setAtuSti(loadStr(64));
			sipSrvcc.setCMsisdn(TBCDUtil.toTBCD(loadByte(16)));
//			sipSrvcc.setSsi(loadHexStr(16));

			//----------2.4.0规范新加字段-start-----------
			sipSrvcc.setRetryAfter(loadInt(4)); 
			sipcall.setReleasePart(loadInt(1)); 
			sipcall.setUserAgent(loadStr(32)); 
			//----------2.4.0规范新加字段-end-----------
			sipSrvcc.setInsertDate(format.format(new Date()));
			break;
		case 7:
			break;
		case 8:
			break;
		default:
			loadByte(SSGMsg.getTotalLength() - SSGMsg.getLength() - 1);
			logger.error("========procedureType 流程乐享异常，进default 判断逻辑======:"+procedureType);
			break;
		}

	}

	public int getProcedureType() {
		return procedureType;
	}

	public void setProcedureType(int procedureType) {
		this.procedureType = procedureType;
	}

	public SipCallEntity getSipcall() {
		return sipcall;
	}

	public void setSipcall(SipCallEntity sipcall) {
		this.sipcall = sipcall;
	}

	public SipRegEntity getSipreg() {
		return sipreg;
	}

	public void setSipreg(SipRegEntity sipreg) {
		this.sipreg = sipreg;
	}


	public SipSrvccEntity getSipSrvcc() {
		return sipSrvcc;
	}

	public void setSipSrvcc(SipSrvccEntity sipSrvcc) {
		this.sipSrvcc = sipSrvcc;
	}

}